<?php

/**
 * comment_fields actions.
 *
 * @package    master_directory
 * @subpackage comment_fields
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class comment_fieldsActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->form = new CommentFieldForm();
  	
  	$this->template = Doctrine_Core::getTable('Template')->find(array($request->getParameter("template_id")));
    
    $this->fields = $this->template->getCommentField();
    
    $this->field_types = Doctrine_Core::getTable('FieldType')->createQuery('a')->execute();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->comment_field = Doctrine_Core::getTable('CommentField')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->comment_field);
    
    $this->form = new CommentFieldForm($this->comment_field);
    
    $this->template = $this->comment_field->getTemplate();
    
    $this->field_types = Doctrine_Core::getTable('FieldType')->createQuery('a')->execute();
        
    return $this->renderPartial('comment_field',array('form'=>$this->form, 'template' => $this->template, 'field_types' => $this->field_types));  
    
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new CommentFieldForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $form = new CommentFieldForm();

    $this->processForm($request, $form);
    
    $comment_field_id = $form->getObject()->getId();
    
    $this->template = $form->getObject()->getTemplate();

   	$this->fields = $this->template->getCommentField();
    
  	$this->form = new CommentFieldForm();
  	
  	$this->field_types = Doctrine_Core::getTable('FieldType')->createQuery('a')->execute();
  	
  	$action_str = "Comment Field created ID: " . $comment_field_id;
  	DirectoryUtils::registerTrace($action_str, $this->getUser()->getAttribute("user_id"));
  	
    
    return $this->renderPartial('category_container',array('fields' => $this->fields, 'template' => $this->template, 'form' => $this->form, 'field_types' => $this->field_types)); 
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($comment_field = Doctrine_Core::getTable('CommentField')->find(array($request->getParameter('id'))), sprintf('Object comment_field does not exist (%s).', $request->getParameter('id')));
    $this->form = new CommentFieldForm($comment_field);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($comment_field = Doctrine_Core::getTable('CommentField')->find(array($request->getParameter('id'))), sprintf('Object comment_field does not exist (%s).', $request->getParameter('id')));
    $form = new CommentFieldForm($comment_field);

    $this->processForm($request, $form);

    $comment_field_id = $form->getObject()->getId();
    
    $this->template = $form->getObject()->getTemplate();

   	$this->fields = $this->template->getCommentField();
    
  	$this->form = new CommentFieldForm();
  	
  	$this->field_types = Doctrine_Core::getTable('FieldType')->createQuery('a')->execute();
  	
  	$action_str = "Comment Field updated ID: " . $comment_field_id;
  	DirectoryUtils::registerTrace($action_str, $this->getUser()->getAttribute("user_id"));
  	
    
    return $this->renderPartial('category_container',array('fields' => $this->fields, 'template' => $this->template, 'form' => $this->form, 'field_types' => $this->field_types)); 
  }

  public function executeDelete(sfWebRequest $request)
  {

    $this->forward404Unless($comment_field = Doctrine_Core::getTable('CommentField')->find(array($request->getParameter('id'))), sprintf('Object comment_field does not exist (%s).', $request->getParameter('id')));
    
    $action_str = "Comment field deleted ID: " . $comment_field->getId();
  	DirectoryUtils::registerTrace($action_str, $this->getUser()->getAttribute("user_id"));
  	
  	foreach ($comment_field->getCommentFieldValue() as $value) {
  		$value->delete();
  	}
    $this->template = $comment_field->getTemplate();
    
    $comment_field->delete();
    
    $this->getUser()->setFlash('save', 'deleted', false);


   	$this->fields = $this->template->getCommentField();
    
  	$this->form = new CommentFieldForm();
  	
  	$this->field_types = Doctrine_Core::getTable('FieldType')->createQuery('a')->execute();
  	
    return $this->renderPartial('category_container',array('fields' => $this->fields, 'template' => $this->template, 'form' => $this->form, 'field_types' => $this->field_types)); 
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    /*if ($form->isValid())
    {*/
      $comment_field = $form->save();

       $this->getUser()->setFlash('save', 'saved', false);
      //$this->redirect('comment_fields/edit?id='.$comment_field->getId());
  //  }
    
  }
}
